package com.example.jegotrip.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.jegotrip.entity.UserInfo;

public class UserDBHelper extends SQLiteOpenHelper {
    // 数据库名称
    private static final String DATABASE_NAME = "user.db";
    // 数据库版本
    private static final int DATABASE_VERSION = 1;

    private static UserDBHelper userDBHelper;

    public UserDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, @Nullable int version) {
        super(context, name, factory, version);
    }

    public static synchronized UserDBHelper getInstance(Context context){
        if (userDBHelper == null) {
            userDBHelper = new UserDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        return userDBHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建users表的SQL语句
        String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS users (user_id INTEGER PRIMARY KEY autoincrement," +
                "username TEXT," + //用户名
                "password TEXT," + //密码
                "nickname TEXT," + //昵称
                "age INTEGER)"; //年龄
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级数据库时执行的SQL语句
        db.execSQL("DROP TABLE IF EXISTS users");
        onCreate(db);
    }

    /**
     * 登录
     */
    @SuppressLint("Range")
    public UserInfo login(String username){
        SQLiteDatabase db = getReadableDatabase();
        UserInfo userInfo = null;
        String sql = "select user_id, username, password, nickname, age from users where username = ?";
        String[] selectionArgs = {username};
        Cursor cursor = db.rawQuery(sql, selectionArgs);
        if (cursor.moveToNext()){
            int user_id = cursor.getInt(cursor.getColumnIndex("user_id"));
            String name = cursor.getString(cursor.getColumnIndex("username"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            String nickname = cursor.getString(cursor.getColumnIndex("nickname"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            userInfo = new UserInfo(user_id, name, password, nickname, age);
        }
        cursor.close();
        db.close();
        return userInfo;
    }

    /**
     * 注册
     */
    public int register(String username, String password, String nickname, int age){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", username);
        values.put("password", password);
        values.put("nickname", nickname);
        values.put("age", age);
        String nullColumnHash = "values(null, ?, ?, ?, ?)";
        int insert = (int)db.insert("users", nullColumnHash, values);
        db.close();
        return insert;
    }

    /**
     * 修改密码
     */
    public int updatePassword(String username, String password){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("password", password);
        int update = db.update("users", values, "username=?", new String[]{username});
        db.close();
        return update;
    }
}
